2021-08-31

Map Basics

Cartography

…the study and practice of making maps. Combining science, aesthetics, and technique, cartography builds on the premise that reality can be modeled in ways that communicate spatial information effectively.

So let’s flex some cartographic muscle and avoid cartostrophe

Types of Maps

Here are a sample:

  • Physical maps show landscape features like rivers and land masses
  • Topographic maps show the contours of elevation lines
  • Climate maps can show atmospheric conditions
  • Thematic maps show special topics like election results or even energy usage in areas.

Reference Maps

Columbia University Map

Columbia University Map

Choropleth Maps

New York Times Election Map

New York Times Election Map

3D Maps

3D Terrain Map

3D Terrain Map

Mental Maps

How New Yorkers See The World

How New Yorkers See The World

Interactive Maps

Animal Migration with Climate Change

Animal Migration with Climate Change

Map Elements

  • Title : Should clearly introduce the topic of the map
  • Legend : Gives an understanding of the range, and variation in data (make the units easy to read)
  • North Arrow : Offers orientation for cardinal directions
  • Scale Bar : Shows the scale whether it is large or small scale map and amount of detail to be excepted.1
  • Sources : Give credit where credit is deserved
  • Date : It’s a good practice to date your maps so you remember when it was made
  • Name : Also don’t forget to take credit for your work

Map Elements

Elements of a basic map

Elements of a basic map

tmap Aesthetics Base Layers

  • tm_polygons Create a polygon layer (with borders)
  • tm_symbols Create a layer of symbols
  • tm_lines Create a layer of lines
  • tm_raster Create a raster layer
  • tm_text Create a layer of text labels
  • tm_basemap Create a layer of basemap tiles
  • tm_tiles Create a layer of overlay tiles

tmap Aesthetics Derived Layers

  • tm_fill Create a polygon layer (without borders)
  • tm_borders Create polygon borders
  • tm_bubbles Create a layer of bubbles
  • tm_squares Create a layer of squares
  • tm_dots Create a layer of dots
  • tm_markers Create a layer of markers
  • tm_iso Create a layer of iso/contour lines
  • tm_rgb Create a raster layer of an image

tmap example

# create borders
tm_shape(nycbb) +
  tm_borders() 

tmap example

# create the fill
tm_shape(nycbb) +
  tm_fill() 

tmap example

# combine them
tm_shape(nycbb) +
  tm_fill() +
  tm_borders() 

## tmap example

# or polygons
tm_shape(nycbb) + 
    tm_polygons()

tmap Colors

map1 <- tm_shape(nycbb) + tm_fill(col = "red")
map2 <- tm_shape(nycbb) + tm_fill(col = "red", alpha  = .2)
map3 <- tm_shape(nycbb) + tm_borders(col = "grey")
map4 <- tm_shape(nycbb) + tm_borders(col = "blue", lwd = 0.5, lty = 2)
tmap_arrange(map1, map2, map3, map4)

tmap colors with Data

Classification - Numeric Data

The common classification methods are:

  • Equal Interval will deal with the range it will cut the range into equal sizes
  • Quantile will deal with the population it will cut the population into equal sizes
  • Natural Breaks will algorithmically minimize within-class variance and maximize between-class differences.
  • Manual allows full control to the user
  • Geometric Interval is created using geometric progression, a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio

Classification in Histogram

Classification Methods

Classification Methods

Classification in Maps

Classification Methods on Maps

Classification Methods on Maps

tmap Layout Elements:

  • tm_layout Adjust the layout (main function)
  • tm_legend Adjust the legend
  • tm_view Configure the interactive view mode
  • tm_style Apply a predefined style
  • tm_format Apply a predefined format

layout

tmap Attributes:

  • tm_grid Create grid lines
  • tm_scale_bar Create a scale bar
  • tm_compass Create a map compass
  • tm_credits Create a text for credits
  • tm_logo Create a logo
  • tm_xlab and tm_ylab Create axis labels
  • tm_minimap Create a minimap (view mode only)

Attributes

Exercise 3.1

Make three maps from your usCounties comparing the percent difference in areas.

Make sure to have a north arrow, scale bar, source, legend, and title.

You can add a projection on the fly by specifying the projection attribute in tm_shape too if you want to play with that.

Faceted Map

subway <- read_sf( "../Shapes/SubwayLines") %>% 
    st_transform(2263) %>% mutate(rt_symbol = as.factor(rt_symbol))

subwayPalette <- c("1"='#EE352E', "4"='#00933C', "7"='#B933AD',
                   "A"='#0039A6',"B"='#FF6319', "G"= '#6CBE45', 
                   "J"='#996633',"L"='#A7A9AC',"N"='#FCCC0A')

tm_shape(nycbb) + tm_borders(col = "grey") +
      tm_shape(subway) + 
          tm_lines(col = 'rt_symbol', palette = subwayPalette) +
      tm_facets(by = "rt_symbol", free.coords = FALSE, nrow = 3)

Faceted Map

Animated Maps

you might need image magick

we will use the tmap_animation function to create a gif.

The parameters that are important are: filename is the filename of the video (should be a .gif or .mpg file] delay is the time between images (in 1/100th of a second) so 4 seconds is 400. loop is a logical that determines whether the animation is looped, or an integer value that determines how many times the animation is looped.

Animated Maps

subwayMaps <- tm_shape(nycbb, simplify = 0.8) + 
    tm_borders(col = "grey") +
      tm_shape(subway) + 
          tm_lines(col = 'rt_symbol', palette = subwayPalette) +
      tm_facets(along = "rt_symbol", free.coords = FALSE)

tmap_animation(subwayMaps, filename = "subway.gif",
               delay = 150, loop = TRUE)

Subway Gif

Interactive Maps with Leaflet

Leaflet is a great interactive mapping platform and couldn’t be simpler to add to your rmarkdown document/slides or shiny dashboards. It can deal with sf and sp objects so you don’t have to make any conversions with your spatial data!

basemap

we can set the view and add a provider tile

library(leaflet)
m <- leaflet() %>% 
    setView(lng = -73.9691305, lat = 40.7764627, zoom = 11) %>% 
    addProviderTiles(providers$CartoDB.Positron)
m

adding data

m %>% addPolygons(data = nycbb %>% st_transform(4326)) %>% 
    addPolylines(data = subway %>% st_transform(4326))

Colors with palettes

pal <- colorFactor(palette = c("#EE352E", "#00933C", "#B933AD",
                           "#0039A6", "#FF6319", "#6CBE45",
                           "#996633", "#A7A9AC", "#FCCC0A"), 
               levels = c("1", "4", "7", "A", "B",
                          "G", "J", "L", "N"))

m %>% addPolygons(data = nycbb %>% st_transform(4326),fillColor ="NA") %>% 
    addPolylines(data = subway %>% st_transform(4326),
                 color = ~pal(rt_symbol))

Colors with palettes

Exercise 3.2

Add usCounties to a leaflet map and create a color palette for colorNumeric using one of your percent differences.